package com.xy.weixin.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xy.weixin.tuling.TulingApiProcess;
import com.xy.weixin.tuling.entity.BaseEntity;
import com.xy.weixin.tuling.entity.FlightEntity;
import com.xy.weixin.tuling.entity.InfoEntity;
import com.xy.weixin.tuling.entity.NewsEntity;
import com.xy.weixin.tuling.entity.TrainEntity;

/**
 * 图灵机器人测试
 * 
 */
public class RobotServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public RobotServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("doPost bedin...");
		// 将请求、响应的编码均设置为UTF-8（防止中文乱码）  
        request.setCharacterEncoding("UTF-8");  
        response.setCharacterEncoding("UTF-8");  
        String header = request.getHeader("user-agent");
        System.out.println("header:"+header);
        System.out.println(header.indexOf("Mozilla"));
        System.out.println(header.indexOf("Windows"));
        
        //前台传参
        String content = request.getParameter("content").trim();
        System.out.println("jan.chen ===>>>content:"+content);
        
        //调用图灵机器人API
        TulingApiProcess process = new TulingApiProcess();
		BaseEntity<?> baseEntity = process.getTulingResult(content);
		if (baseEntity != null) {
			switch (baseEntity.getCode()) {
			case 100000:	//文本类数据
				System.out.println("Text:"+baseEntity.getText().trim());
				break;
			case 305000:	//列车
				BaseEntity<TrainEntity> trainEntity = (BaseEntity<TrainEntity>)baseEntity;
				System.out.println("Text:"+trainEntity.getCode());
				//detailurl:http://touch.qunar.com/h5/train/
				for(TrainEntity entity:trainEntity.getList()) {
					System.out.println("trainnum:"+entity.getTrainnum()+" start:"+entity.getStart()+" terminal:"+entity.getTerminal()+
							" starttime:"+entity.getStarttime()+" endtime:"+entity.getEndtime()+" detailurl:"+entity.getDetailurl()+" icon:"+entity.getIcon());
				}
				break;
			case 306000:	//航班
				BaseEntity<FlightEntity> flightEntity = (BaseEntity<FlightEntity>)baseEntity;
				System.out.println("Text:"+flightEntity.getCode());
				//detailurl:http://touch.qunar.com/h5/flight/
				for(FlightEntity entity:flightEntity.getList()) {
					System.out.println("flight:"+entity.getFlight()+" route:"+entity.getRoute()+" starttime:"+entity.getStarttime()+" endtime:"+entity.getEndtime()+
							" state:"+entity.getState()+" detailurl:"+entity.getDetailurl()+" icon:"+entity.getIcon());
				}
				break;
			case 200000:	//网址类数据
				//请点击 <a href="+baseEntity.getUrl()+">打开页面</a>
				System.out.println("Text:"+baseEntity.getText());
				System.out.println("Url:"+baseEntity.getUrl());
				break;	
			case 302000:	//新闻
				BaseEntity<NewsEntity> newsEntity = (BaseEntity<NewsEntity>)baseEntity;
				System.out.println("Text:"+newsEntity.getCode());
				for(NewsEntity entity:newsEntity.getList()) {
					System.out.println("article:"+entity.getArticle()+" source:"+entity.getSource()+" detailurl:"+entity.getDetailurl()+" icon:"+entity.getIcon());
				}
				break;
			case 308000:	//菜谱、视频、小说
				BaseEntity<InfoEntity> infoEntity = (BaseEntity<InfoEntity>)baseEntity;
				System.out.println("Text:"+infoEntity.getCode());
				for(InfoEntity entity:infoEntity.getList()) {
					System.out.println("name:"+entity.getName()+" info:"+entity.getInfo()+" detailurl:"+entity.getDetailurl()+" icon:"+entity.getIcon());
				}
				break;
			default:	//其他
				break;
			}
		}else {
			
			System.out.println("你说的太高深了哦！o(╯□╰)o");
		}
		request.setAttribute("info", baseEntity);
		request.getRequestDispatcher("/result.jsp").forward(request, response);
	}

}
